import express from 'express';
import { authenticateToken } from '../middleware/auth.js';
import User from '../models/user.js';

const router = express.Router();

router.get('/recent-views', authenticateToken, async (req, res) => {
  try {
    const userId = req.user.id;
    const recentViews = await User.getRecentViews(userId);
    res.json(recentViews);
  } catch (error) {
    console.error('获取最近浏览记录失败:', error);
    res.status(500).json({ message: '获取最近浏览记录失败' });
  }
});

router.post('/record-view', authenticateToken, async (req, res) => {
  try {
    const userId = req.user.id;
    const { productId } = req.body;
    
    if (!productId) {
      return res.status(400).json({ message: '商品ID不能为空' });
    }
    
    await User.recordProductView(userId, productId);
    res.json({ success: true });
  } catch (error) {
    console.error('记录浏览历史失败:', error);
    res.status(500).json({ message: '记录浏览历史失败' });
  }
});

export default router; 